// 连接 websoket
const connect = (url, opt) => {
	const { open, msg } = opt || {}
	let sse
	if (window?.EventSource) {
		sse = new EventSource(url);
	}else{
		throw new Error('当前浏览器不支持 SSE')
	}
	// 连接成功
	sse.onopen =  () => {
		console.log('SSE 连接成功')
		open?.(sse)
	}
	// 从服务器接受到信息时
	sse.onmessage = function (event) {
		const data = event.data
		console.log(data)
		msg?.(data)
	}
	// 连接失败
	sse.onerror = () => {
		console.error('SSE 连接出错')
	}
	return sse
}
// 重连
// const reconnect = (url, opt) => setTimeout( () => connect(url, opt), 4000)
export default connect